/*
 * @Author: your name
 * @Date: 2021-06-27 13:21:22
 * @LastEditTime: 2021-07-04 21:38:34
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue_shop_mananger\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'

import login from '../views/login/login.vue'
import home from '../views/home/home.vue'

import welcome from '../views/home/componets/welcome.vue'
import users from '../components/users/users.vue'
import roles from '../components/roles/roles.vue'
import rights from '../components/rights/rights.vue'
import params from '../components/params/params.vue'
import goods from '../components/goods/goods.vue'
import categories from '../components/categories/categories.vue'
Vue.use(VueRouter)

const routes = [
  {path: '/',redirect:'/login'},
  {path:'/login',component:login},
  {path:'/home',
   component:home,
   redirect:'/welcome',
   children:[
     {path:'/welcome',component:welcome},
     {path:'/users',component:users},
     {path:'/roles',component:roles},
     {path:'/rights',component:rights},
     {path:'/params',component:params},
     {path:'/goods',component:goods},
     {path:'/categories',component:categories},
   ]
   },
]

const router = new VueRouter({
  routes
})
//挂在路由导航守卫
router.beforeEach((to, from, next) => {
 //to 将要访问的路径
 //from 代表从哪个路径来的
 //next 一个函数，是否放行
 if(to.path ==='/login')return next();
 const tokenStr=window.sessionStorage.getItem('token');
 if (!tokenStr) {return next('/login')}
 next();
})

export default router
